package com.caifu.controller;


import com.caifu.pojo.SysUser;
import com.caifu.service.CDUserService;
import com.caifu.util.Utils;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人员设备关联表 前端控制器
 * </p>
 *
 * @author tww
 * @since 2020-12-10
 */
@Controller
@RequestMapping("center/user")
public class CDUserController extends AbstractController{
    final static Logger logger = LoggerFactory.getLogger(CDUserController.class);

    @Resource
    private CDUserService cdUserService;

    /**
     * 下发列表
     * @param model
     * @return
     * @throws Exception
     */
    @GetMapping(value = "list")
    public String list(ModelMap model) throws Exception{
            return "view/device/user_list";
    }

    /**
     *
     * @param currentPage
     * @param nums
     * @param deviceName
     * @param state
     * @return
     * @throws Exception
     */
    @GetMapping(value = "userList")
    @ResponseBody
    public JSONObject userList(int currentPage, int nums, String userName, String deviceName, String orgId, String state) throws Exception {
        SysUser user = getUser();
        JSONObject obj = new JSONObject();
        try {
            Map map = new HashMap();
            map.put("page", currentPage);
            map.put("pageSize", nums);
            if(Utils.isNotNull(deviceName)){
                map.put("deviceName", "%"+deviceName+"%");
            }
            if(Utils.isNotNull(userName)){
                map.put("userName", "%"+userName+"%");
            }

            map.put("orgId", orgId);
            map.put("state", state);
            int total = cdUserService.countDeviceUser(map);
            List<Map<String, Object>> list = cdUserService.listDeviceUser(map);
            int i = (currentPage-1)*nums +1;
            for (Map<String, Object> maps : list) {
                maps.put("num",i);
                i++;
            }
            obj.put("data", list);
            obj.put("count", total);
            obj.put("code",0);
            obj.put("msg","");
        } catch (Exception e) {
            e.printStackTrace();
            obj.put("code",301);
            obj.put("msg","查询异常");
        }
        return obj;
    }


}
